#include <ProcessComposition.h>
#include <shared_data.h>
#include <Scheduler.h>
#include <LocalStoreManager.h>
#include <csp_protocol_msg.h>
#include <WaitQueueEvent.h>



LocalStoreManager::LocalStoreManager(int spe_id_){
	slot_idx = 0;
	for(int i = 0; i < MAX_SPU_SLOTS; i++){
		spe_slots[i] = NULL;
	}
	spe_id = spe_id_;
	scheduler = Scheduler::Instance();
	count = 0;
}


ProcessComposition *
LocalStoreManager::Get(){
	ProcessComposition *p;
	//for(int i = 0; i < MAX_SPU_SLOTS; i++){
	//	if(NULL == spe_slots[i]){
	//		p = scheduler->get(i, spe_id);	
	//		if(NULL != p){
	//			count++;
	//			spe_slots[i] = p;
	//			return p;
	//		}
	//	}	
	//}
	if(NULL == spe_slots[0]){
		p = scheduler->get(0, spe_id);	
		if(NULL != p){
			count++;
			spe_slots[0] = p;
			return p;
		}
	}	
	if(NULL == spe_slots[1]){
		p = scheduler->get(1, spe_id);	
		if(NULL != p){
			count++;
			spe_slots[1] = p;
			return p;
		}
	}	



	return NULL;
}


/* 
 *
 * */
void
LocalStoreManager::Put(ProcessComposition &p, int state){
	spe_slots[p.GetMemorySlot()] = NULL;
	count--;
	/* If process is waiting then don't schedule process */
	if( STATE_WAIT == state || STATE_CHAN_WAIT == state ){
		/*
		   p.SetLast(2, spe_id);
		   */
		/* pass */
	}else{
		/*
		   p.SetLast(1, spe_id);
		   */
		scheduler->put(p , state);
	}
}


